using System.Windows.Forms;
using Nuwanda.TfsSpotlight.Shell.Proxies.Interfaces;
using Nuwanda.TfsSpotlight.Shell.Services;

namespace Nuwanda.TfsSpotlight.Shell.Proxies
{
    internal class ToolStripProxy : IToolStrip
    {
        private readonly ToolStrip _toolStrip;

        public ControlInvoker ToolStripInvoker { get; private set; }

        public ToolStripProxy(ToolStrip toolStrip)
        {
            this._toolStrip = toolStrip;
            this.ToolStripInvoker = new ControlInvoker(toolStrip);
        }

        public void Clear()
        {
            this.ToolStripInvoker.RunInControlsThread(
                () => this._toolStrip.Items.Clear());
        }

        public void Add(CommandConfiguration commandConfiguration)
        {
            this.ToolStripInvoker.RunInControlsThread(
                delegate
                    {
                        var item = commandConfiguration.ToToolStripItem();
                        if (item == null) return;

                        this._toolStrip.Items.Add(item);
                    });
        }

        public void Add(ToolStripItem item)
        {
            this.ToolStripInvoker.RunInControlsThread(
                () => this._toolStrip.Items.Add(item));
        }

        public bool Visible
        {
            get { return this.ToolStripInvoker.RunAndReturnInControlsThread(() => this._toolStrip.Visible); }
            set { this.ToolStripInvoker.RunInControlsThread(() => this._toolStrip.Visible = value); }
        }
    }
}